<?php
//strlen\mb_strlen字符串长度
$a='123计算机';
echo strlen($a),"\n";//字节的个数 // 12
echo mb_strlen($a),"\n";//字符的个数 //6
$b=iconv('utf-8','GBK',$a);
echo strlen($b),"\n";// 9 字符串显示时的宽度，1个汉字是两个英文字符的宽度
//strcmp用于比较两个字符串的大小
echo strcmp("Hello","Hi"),"\n"; //-1
echo strcasecmp("hi","Hi"),"\n"; //0
echo strcasecmp("hi~","Hi"),"\n"; //1
//trim删除字符串首尾的空白
$s="   \t \t How are you?  \t  ";
echo "[$s]\n"; //[   	 	 How are you?  	  ]
echo "[", trim($s),"]\n"; //[How are you?]
echo "[", ltrim($s),"]\n"; //[How are you?  	  ]
echo "[", rtrim($s),"]\n"; //[   	 	 How are you?]
$s=",:.How are you?____";
echo "[", trim($s,"_,:."),"]\n"; //[How are you?]
//strpos查找子串在父字符中的位置，i忽略大小写，r相反的顺序
echo strpos('计算机程序设计','程序'),"\n";   // 显示 9
echo stripos("I'm Happy.",'h'),"\n";   // 显示 4
echo strripos("How old are you?",'o'),"\n";   // 显示 13
$p=0;
echo $p=strpos("How old are you?",'o', $p),"\n";   // 显示1
echo $p=strpos("How old are you?",'o', $p+1),"\n";   // 显示4
echo $p=strpos("How old are you?",'o', $p+1),"\n";   // 显示13

var_dump(strpos('计算机程序设计','小程序'));   //bool(false) 没有找到
//bin2hex将字符串的每个字节依次转换为两个16进制字符
$a='123计算机';
echo bin2hex($a),"\n"; //313233e8aea1e7ae97e69cba
//chunk_split将字符串按指定的长度用指定的字符分隔开来
echo chunk_split(bin2hex($a),2,'_'),"\n"; //31_32_33_e8_ae_a1_e7_ae_97_e6_9c_ba_
//str_replace字符串替换
echo str_replace('o','ooo',"How are you?\n");//Hooow are yooou?
echo str_replace(['o', 'r'],'oo',"How are you?\n");//Hoow aooe yoou?
echo str_replace(['o', 'r'],['O','R'],"How are you?\n");//HOw aRe yOu?
echo str_replace(['o','r',' '],'',"How are you?\n");//Hwaeyu?
print_r(str_replace(['o', 'r'],['O','R'],["How are you?",'So good']));
/*
Array
(
    [0] => HOw aRe yOu?
    [1] => SO gOOd
) */
//str_repeat重复字符串若干次, 0次会返回空字符串""
echo str_repeat('Ok',3),"\n"; // OkOkOk
echo str_repeat('-',60),"\n";
//join 拼接数组元素为一个字符串
echo join(['Hi','Zhangsan']),"\n"; //HiZhangsan
echo join(', ', [123,456,789]),"\n"; //123, 456, 789
//strtolower、strtoupper转换大小写
echo strtolower("How Are You.\n");//how are you.
echo strtoupper("I'm fine, Thank you.\n"); //I'M FINE, THANK YOU.
//printf、sprintf格式化输出
$a=16.38759;
printf("[%f] [%1$.2f] [%1$6.2f] [%1$-6.2f]\n",$a); //[16.387590] [16.39] [ 16.39] [16.39 ]
$a="How are you?";
printf("[%s] [%1$.3s] [%1$5.3s] [%1$'*6.3s]\n",$a); //[How are you?] [How] [  How] [***How]
$a="计算机";
printf("[%s] [%1$6.3s]\n",$a);//[计算机] [   计]
//substr取子串
echo substr("Hello",0,2),"\n"; //He
echo substr("Hello",0,-2),"\n"; //Hel
echo substr("Hello",-4,-2),"\n"; //el
//str_starts_with是否以指定的串开头
var_dump(str_starts_with('Hello',"He"));//bool(true)
var_dump(str_starts_with('Hello',"he"));//bool(false)
var_dump(str_ends_with('Hello',"o"));//bool(true)